package com.qiyu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiyu.entity.FirstLevelMenu;
import com.qiyu.entity.HomeBanner;
import com.qiyu.entity.HomeSetting;
import com.qiyu.model.request.backend.HomeSettingReq;

import com.qiyu.model.response.front.FirstLevelMenuRes;
import com.qiyu.model.response.front.HomeBannerRes;
import com.qiyu.model.response.front.HomeRes;
import com.qiyu.model.response.front.HomeSettingRes;
import com.qiyu.service.FirstLevelMenuService;
import com.qiyu.service.HomeBannerService;
import com.qiyu.service.HomeService;
import com.qiyu.service.HomeSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeBannerService homeBannerService;
    @Autowired
    private HomeSettingService homeSettingService;
    @Autowired
    private FirstLevelMenuService firstLevelMenuService;
    @Override
    public HomeRes queryHome() {
        List<HomeBannerRes> homeBannerRes = this.queryBanner();
        HomeSettingRes homeSettingRes = this.querySetting();
        List<FirstLevelMenuRes> firstLevelMenuRes = this.queryFirstLevelMenu();

        HomeRes homeRes = HomeRes.builder()
                .firstLevelMenus(firstLevelMenuRes)
                .homeBanners(homeBannerRes)
                .homeSetting(homeSettingRes)
                .build();
        return homeRes;
    }
    private List<FirstLevelMenuRes> queryFirstLevelMenu() {
        //一级菜单
        List<FirstLevelMenu> firstLevelMenus = firstLevelMenuService.queryAll();
        List<FirstLevelMenuRes> firstLevelMenuRes = new ArrayList<>();
        for(FirstLevelMenu firstLevelMenu:firstLevelMenus){
            FirstLevelMenuRes  menuRes = new FirstLevelMenuRes();
            BeanUtils.copyProperties(firstLevelMenu,menuRes);
            firstLevelMenuRes.add(menuRes);
        }
        return  firstLevelMenuRes;
    }

    private HomeSettingRes querySetting() {
        HomeSettingReq homeSettingReq = new HomeSettingReq();
        //关于我们
        homeSettingReq.setHandler("query");
        HomeSetting homeSetting = homeSettingService.saveSetting(homeSettingReq);
        HomeSettingRes homeSettingRes = new HomeSettingRes();
        BeanUtils.copyProperties(homeSetting,homeSettingRes);
        return homeSettingRes;
    }

    private List<HomeBannerRes> queryBanner(){
        IPage<HomeBanner> homeBannerIPage =homeBannerService.queryAll(1,10000,"");
        List<HomeBannerRes> bannerResList = new ArrayList<>();
        for(HomeBanner homeBanner:homeBannerIPage.getRecords()){
            HomeBannerRes homeBannerRes = new HomeBannerRes();
            homeBannerRes.setBannerName(homeBanner.getBannerName());
            homeBanner.setBannerUrl( homeBanner.getBannerUrl());
            bannerResList.add(homeBannerRes);
        }
        return bannerResList;
    }
}
